home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / curses.c < prev    next >
C/C++ Source or Header  |  1994-01-10  |  24KB  |  1,122 lines

  1. /* curses.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    14407 SW Teal Blvd. #C
  6.  *    Beaverton, OR 97005
  7.  *    kirkenda@cs.pdx.edu
  8.  */
  9.  
  10.  
  11. /* This file contains the functions & variables needed for a tiny subset of
  12.  * curses.  The principle advantage of this version of curses is its
  13.  * extreme speed.  Disadvantages are potentially larger code, few supported
  14.  * functions, limited compatibility with full curses, and only stdscr.
  15.  */
  16.  
  17. #include "config.h"
  18. #include "vi.h"
  19.  
  20. #if ANY_UNIX || OS2
  21. # if UNIXV || COH_386 || OS2
  22. #  if TERMIOS
  23. #   ifdef AIX
  24. #    define _XOPEN_SOURCE
  25. #    define _ALL_SOURCE
  26. #   endif
  27. #   include    <termios.h>
  28. #   ifdef AIX
  29. #    undef _XOPEN_SOURCE
  30. #    undef _ALL_SOURCE
  31. #   endif
  32. #  else
  33. #   include    <termio.h>
  34. #  endif
  35. #  if !OS2
  36. #   ifndef NO_S5WINSIZE
  37. #    ifdef NEED_PTEM
  38. #     include    <sys/stream.h>    /* winsize struct defined in one of these? */
  39. #     include    <sys/ptem.h>
  40. #    endif
  41. #   else
  42. #    undef    TIOCGWINSZ    /* we can't handle it correctly yet */
  43. #   endif
  44. #  endif
  45. # else
  46. #  if TERMIOS
  47. #   include    <termios.h>
  48. #  else
  49. #   ifdef hpux
  50. #    include    <termio.h>
  51. #   else
  52. #    include    <sgtty.h>
  53. #   endif
  54. #  endif
  55. # endif
  56. #endif
  57.  
  58. #if TOS
  59. # include    <osbind.h>
  60. #endif
  61.  
  62. #if OSK
  63. # include    <sgstat.h>
  64. #endif
  65.  
  66. #if VMS
  67. extern int VMS_read_raw;  /* Set in initscr() */
  68. #endif
  69.  
  70.  
  71. static void     starttcap P_((char *));
  72.  
  73. /* variables, publicly available & used in the macros */
  74. char    *termtype;    /* name of terminal entry */
  75. short    ospeed;        /* speed of the tty, eg B2400 */
  76. #if OSK
  77. char    PC_;    /* Pad char */
  78. char    *BC;    /* backspace character string */
  79. #else
  80. char    PC;        /* Pad char */
  81. #endif
  82. WINDOW    *stdscr;    /* pointer into kbuf[] */
  83. WINDOW    kbuf[KBSIZ];    /* a very large output buffer */
  84. int    LINES;        /* :li#: number of rows */
  85. int    COLS;        /* :co#: number of columns */
  86. int    AM;        /* :am:  boolean: auto margins? */
  87. int    PT;        /* :pt:  boolean: physical tabs? */
  88. char    *VB;        /* :vb=: visible bell */
  89. char    *UP;        /* :up=: move cursor up */
  90. char    *SO = "";    /* :so=: standout start */
  91. char    *SE = "";    /* :se=: standout end */
  92. char    *US = "";    /* :us=: underline start */
  93. char    *UE = "";    /* :ue=: underline end */
  94. char    *MD = "";    /* :md=: bold start */
  95. char    *ME = "";    /* :me=: bold end */
  96. char    *AS = "";    /* :as=: alternate (italic) start */
  97. char    *AE = "";    /* :ae=: alternate (italic) end */
  98. #ifndef NO_VISIBLE
  99. char    *MV;        /* :mv=: "visible" selection start */
  100. #endif
  101. char    *CM;        /* :cm=: cursor movement */
  102. char    *CE;        /* :ce=: clear to end of line */
  103. char    *CD;        /* :cd=: clear to end of screen */
  104. char    *AL;        /* :al=: add a line */
  105. char    *DL;        /* :dl=: delete a line */
  106. #if OSK
  107. char    *SR_;        /* :sr=: scroll reverse */
  108. #else
  109. char    *SR;        /* :sr=: scroll reverse */
  110. #endif
  111. char    *KS = "";    /* :ks=: switch keypad to application mode */
  112. char    *KE = "";    /* :ke=: switch keypad to system mode */
  113. char    *KU;        /* :ku=: key sequence sent by up arrow */
  114. char    *KD;        /* :kd=: key sequence sent by down arrow */
  115. char    *KL;        /* :kl=: key sequence sent by left arrow */
  116. char    *KR;        /* :kr=: key sequence sent by right arrow */
  117. char    *HM;        /* :HM=: key sequence sent by the <Home> key */
  118. char    *EN;        /* :EN=: key sequence sent by the <End> key */
  119. char    *PU;        /* :PU=: key sequence sent by the <PgUp> key */
  120. char    *PD;        /* :PD=: key sequence sent by the <PgDn> key */
  121. char    *KI;        /* :kI=: key sequence sent by the <Insert> key */
  122. char    *kD;        /* :kD=: key sequence sent by the <Delete> key */
  123. #ifndef NO_FKEY
  124. char    *FKEY[NFKEYS];    /* :k0=: ... :k9=: sequences sent by function keys */
  125. #endif
  126. char    *IM = "";    /* :im=: insert mode start */
  127. char    *IC = "";    /* :ic=: insert the following character */
  128. char    *EI = "";    /* :ei=: insert mode end */
  129. char    *DC;        /* :dc=: delete a character */
  130. char    *TI = "";    /* :ti=: terminal init */    /* GB */
  131. char    *TE = "";    /* :te=: terminal exit */    /* GB */
  132. #ifndef NO_CURSORSHAPE
  133. #if 1
  134. char    *CQ = (char *)0;/* :cQ=: normal cursor */
  135. char    *CX = (char *)1;/* :cX=: cursor used for EX command/entry */
  136. char    *CV = (char *)2;/* :cV=: cursor used for VI command mode */
  137. char    *CI = (char *)3;/* :cI=: cursor used for VI input mode */
  138. char    *CR = (char *)4;/* :cR=: cursor used for VI replace mode */
  139. #else
  140. char    *CQ = "";    /* :cQ=: normal cursor */
  141. char    *CX = "";    /* :cX=: cursor used for EX command/entry */
  142. char    *CV = "";    /* :cV=: cursor used for VI command mode */
  143. char    *CI = "";    /* :cI=: cursor used for VI input mode */
  144. char    *CR = "";    /* :cR=: cursor used for VI replace mode */
  145. #endif
  146. #endif
  147. char    *aend = "";    /* end an attribute -- either UE or ME */
  148. char    ERASEKEY;    /* backspace key taken from ioctl structure */
  149. #ifndef NO_COLOR
  150. char    normalcolor[24];
  151. char    SOcolor[24];
  152. char    SEcolor[24];
  153. char    UScolor[24];
  154. char    UEcolor[24];
  155. char    MDcolor[24];
  156. char    MEcolor[24];
  157. char    AScolor[24];
  158. char    AEcolor[24];
  159. char    Qcolor[24];
  160. # ifndef NO_POPUP
  161. char    POPUPcolor[24];
  162. # endif
  163. # ifndef NO_VISIBLE
  164. char    VISIBLEcolor[24];
  165. # endif
  166. #endif
  167.  
  168. #if ANY_UNIX || OS2
  169. # if UNIXV || COH_386 || hpux || OS2
  170. #  if TERMIOS
  171. static struct termios    oldtermio;    /* original tty mode */
  172. static struct termios    newtermio;    /* cbreak/noecho tty mode */
  173. #  else
  174. static struct termio    oldtermio;    /* original tty mode */
  175. static struct termio    newtermio;    /* cbreak/noecho tty mode */
  176. #  endif
  177. # else
  178. static struct sgttyb    oldsgttyb;    /* original tty mode */
  179. static struct sgttyb    newsgttyb;    /* cbreak/nl/noecho tty mode */
  180. static int        oldint;        /* ^C or DEL, the "intr" character */
  181. #  ifdef TIOCSLTC
  182. static int        oldswitch;    /* ^Z, the "suspend" character */
  183. static int        olddswitch;    /* ^Y, the "delayed suspend" char */
  184. static int        oldquote;    /* ^V, the "quote next char" char */
  185. #  endif
  186. # endif
  187. #endif
  188.  
  189. #if OSK
  190. static struct sgbuf    oldsgttyb;    /* orginal tty mode */
  191. static struct sgbuf    newsgttyb;    /* noecho tty mode */
  192. #endif
  193.  
  194.  
  195. /* This boolean variable indicated whether the termcap description is
  196.  * sufficient to support visual mode.
  197.  */
  198. int    canvi = TRUE;        /* boolean: know enough for visual mode? */
  199.  
  200. static char    *capbuf;    /* capability string buffer */
  201.  
  202. /* Initialize the Curses package. */
  203. void initscr()
  204. {
  205.     /* create stdscr */
  206.     stdscr = kbuf;
  207.  
  208.     /* make sure TERM variable is set */
  209.     termtype = getenv("TERM");
  210.  
  211. #if OS2
  212.     /* get initial video state; turn on ANSI */
  213.     v_vio_init();
  214. #endif
  215. #if VMS
  216.     /* VMS getenv() handles TERM as a environment setting.  Foreign 
  217.      * terminal support can be implemented by setting the ELVIS_TERM
  218.      * logical or symbol to match a tinytcap entry.
  219.      */
  220.     if (!strcmp(termtype,"unknown"))
  221.         termtype = getenv("ELVIS_TERM");
  222. #endif
  223. #if MSDOS
  224.     /* For MS-DOS, if TERM is unset we can default to "pcbios", or
  225.      * maybe "rainbow".
  226.      */
  227.     if (!termtype)
  228.     {
  229. #ifdef RAINBOW
  230.         if (*(unsigned char far*)(0xffff000eL) == 6   /* Rainbow 100a */
  231.          || *(unsigned char far*)(0xffff000eL) == 148)/* Rainbow 100b */
  232.         {
  233.             termtype = "rainbow";
  234.         }
  235.         else
  236. #endif
  237.             termtype = "pcbios";
  238.     }
  239.     if (!strcmp(termtype, "pcbios"))
  240. #else
  241.     if (!termtype)
  242. #endif
  243.     {
  244. #if ANY_UNIX || OSK
  245.         termtype = "unknown";
  246.         starttcap(termtype);
  247. #endif
  248. #if AMIGA
  249.         termtype = TERMTYPE;
  250.         starttcap(termtype);
  251. #endif
  252. #if MSDOS
  253.         starttcap("pcbios");
  254. #endif
  255. #if OS2
  256.         termtype = "ansi";
  257.         starttcap(termtype);
  258. #endif
  259. #if TOS
  260.         termtype = "vt52";
  261.         starttcap(termtype);
  262. #endif
  263. #if VMS
  264.         write(2, "UNKNOWN terminal: define ELVIS_TERM\n", (unsigned)36);
  265.         exit(2);
  266. #endif
  267.     }
  268.     else
  269.     {
  270. #if MSDOS
  271.         *o_pcbios = 0;
  272. #endif
  273.         /* start termcap stuff */
  274.         starttcap(termtype);
  275.     }
  276.  
  277.     /* change the terminal mode to cbreak/noecho */
  278. #if ANY_UNIX || OS2
  279. # if UNIXV || COH_386 || hpux || OS2
  280. #  if TERMIOS
  281.     tcgetattr(2, &oldtermio);
  282. #  else
  283.     ioctl(2, TCGETA, &oldtermio);
  284. #  endif
  285. # else
  286.     ioctl(2, TIOCGETP, &oldsgttyb);
  287. # endif
  288. #endif
  289.  
  290. #if OSK
  291.     _gs_opt(0, &oldsgttyb);
  292. #endif
  293.  
  294. #if VMS
  295.     VMS_read_raw = 1;   /* cbreak/noecho */
  296.     vms_open_tty();
  297. #endif
  298.     resume_curses(TRUE);
  299. }
  300.  
  301. /* Shut down the Curses package. */
  302. void endwin()
  303. {
  304.     /* change the terminal mode back the way it was */
  305.     suspend_curses();
  306. #if AMIGA
  307.     amiclosewin();
  308. #endif
  309. #if OS2
  310.     v_vio_restore();    /* restore initial video state */
  311. #endif
  312. }
  313.  
  314.  
  315. static int curses_active = FALSE;
  316.  
  317. extern int oldcurs;
  318.  
  319. /* Send any required termination strings.  Turn off "raw" mode. */
  320. void suspend_curses()
  321. {
  322. #if ANY_UNIX && !(UNIXV || COH_386 || hpux)
  323.     struct tchars    tbuf;
  324. # ifdef TIOCSLTC
  325.     struct ltchars    ltbuf;
  326. # endif
  327. #endif
  328. #ifndef NO_CURSORSHAPE
  329.     if (has_CQ)
  330.     {
  331.         do_